//
//  MyMessageController.m
//  FinancialProduct
//
//  Created by Stella on 2017/8/30.
//  Copyright © 2017年 maq. All rights reserved.
//
#import "HandleNetProduct.h"
#import "MyAccountCell.h"
#import "MJRefresh.h"
#import "MsgModel.h"
#import "UserModel.h"
#import "MJExtension.h"
#import "MyMessageController.h"

@interface MyMessageController (){
    int count;
}
@property (weak, nonatomic) IBOutlet UIImageView *bgImage;
@property (weak, nonatomic) IBOutlet UITableView *mTableView;
@property (weak, nonatomic) IBOutlet UIButton *leftBt;
@property (weak, nonatomic) IBOutlet UIButton *rightBt;
@property (weak, nonatomic) IBOutlet UIImageView *noDataImage;
@property (weak, nonatomic) IBOutlet UILabel *reminder;
@property (weak, nonatomic) IBOutlet UIView *noDataView;
@property (strong,nonatomic) NSArray *DataArray;
@end

@implementation MyMessageController

- (void)viewDidLoad {
    [super viewDidLoad];
//    self.DataArray=@[@"修改密码成功",@"修改密码成功",@"修改密码成功",@"修改密码成功"];
    self.leftBt.selected=YES;
    self.rightBt.selected=NO;
    self.noDataView.hidden=YES;
   
//    if (self.DataArray.count==0) {
//        self.noDataView.hidden=NO;
//        self.reminder.text=@"没有未读消息";
//    }
    self.bgImage.image=[UIImage imageNamed:@"1-1.png"];
    _mTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        count=1;
        [self getMSGList];
    }];
    _mTableView.mj_footer =[MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        count++;
        [self getMSGList];
    }];
    
    // 马上进入刷新状态
    [self.mTableView.mj_header beginRefreshing];
    
}
//获取已读未读列表
-(void)getMSGList{

    NSString *isread;
    if (self.leftBt.selected) {
        isread=@"N";
    }else{
        isread=@"Y";
    }
    UserModel *model=[UserModel getUser];
    int mid=[model.CustomerID intValue];
    NSDictionary *params=@{@"AAAMethodName":@"Infomyinfo",@"CustomerID":@(mid),@"IsRead":isread,@"PageIndex":@(count),@"PageSize":@(20),@"AccountStatus":model.Accountstatus};
    //NSLog(@"getMSGList params %@",params);
    [HandleNetProduct getInvestAssetListforParams:params Success:^(NSArray *data) {
        [self.mTableView.mj_header endRefreshing];
        [self.mTableView.mj_footer endRefreshing];
        self.DataArray=data;
        
        if (self.DataArray.count==0) {
            self.noDataView.hidden=NO;
            if (self.leftBt.selected) {
                self.reminder.text=@"没有未读消息";
            }else{
                self.reminder.text=@"没有已读消息";
            }
            
        }
        [self.mTableView reloadData];
    } Failure:^(NSString *errorInfo) {
        [self.mTableView.mj_header endRefreshing];
        [self.mTableView.mj_footer endRefreshing];
    }];
}
-(void)setMsgIsReadNOtoYES:(NSInteger)index{
    NSDictionary *dict=self.DataArray[index];
    NSLog(@"setisReadNOtoYES ==%@",dict);
    UserModel *model=[UserModel getUser];
    int mid=[model.CustomerID intValue];
    NSDictionary *params=@{@"AAAMethodName":@"Infomyinfo",@"CustomerID":@(mid),@"IsRead":@"NtoY",@"WebmessageID":dict[@"WebmessageID"],@"AccountStatus":model.Accountstatus};
    NSLog(@"params---%@",params);
    [HandleNetProduct changeMyMessagesStatusforParams:params Success:^(NSDictionary *data) {
        [self getMSGList];
    } Failure:^(NSString *errorInfo) {
        
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)changTag:(UIButton *)sender {
    self.noDataView.hidden=YES;
    if (sender.tag==0) {
        self.leftBt.selected=YES;
        self.rightBt.selected=NO;
        self.bgImage.image=[UIImage imageNamed:@"1-1.png"];
//        self.DataArray=@[];
        
//        if (self.DataArray.count==0) {
//            self.noDataView.hidden=NO;
//            self.reminder.text=@"没有未读消息";
//        }
    }else{
        self.leftBt.selected=NO;
        self.rightBt.selected=YES;
        
        self.bgImage.image=[UIImage imageNamed:@"2-2.png"];
//        self.DataArray=@[@"注册成功",@"注册成功",@"注册成功"];
//        if (self.DataArray.count==0) {
//            self.noDataView.hidden=NO;
//            self.reminder.text=@"没有已读消息";
//        }
    }
   
    [self getMSGList];
    
    
}

#pragma mark - Table view data source
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50
    ;
    
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    return 1;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return self.DataArray.count;
  
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    MyAccountCell *cell=[tableView dequeueReusableCellWithIdentifier:@"MyAccountCell" forIndexPath:indexPath];
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    cell.accessoryType=UITableViewCellAccessoryNone;
    if (self.leftBt.selected) {
        cell.mImage.image=[UIImage imageNamed:@"mail-2"];
        cell.NameLabel.textColor=[UIColor blackColor];
    }else{
        cell.mImage.image=[UIImage imageNamed:@"mail"];
        cell.NameLabel.textColor=[UIColor darkGrayColor];
    }
    
    MsgModel *msgM=[MsgModel mj_objectWithKeyValues:self.DataArray[indexPath.row]];
    cell.NameLabel.text=msgM.Webmessage;
    cell.RightLabel.text=msgM.DateTime;
    return cell;

}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 0.01f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01f;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.leftBt.selected) {
        [self setMsgIsReadNOtoYES:indexPath.row];
    }

    
}

@end
